#ifndef FORMCFGRULESET_H
#define FORMCFGRULESET_H

#include <QHideEvent>
#include <QPointer>
#include <QShowEvent>
#include <QSortFilterProxyModel>
#include <QSqlRecord>
#include <QSqlTableModel>
#include <QTableView>
#include <QVariant>
#include <QWidget>

#include "rulesettableview.h"
#include "rulesetsqltablemodel.h"
#include "formdlgruleset.h"



namespace Ui {
class FormCfgRuleset;
}


/**
\file formcfgruleset.h
\class FormCfgRuleset

\brief Browse Rulesets and call add amend delete ruleset form

\todo Display rulesets and rulesnippets in alaphabetical order of name

\todo If rulesets and rulesnippets are displayed in alphabetical order
      then ensure correct line is selected after adding/amending
***************************/
class FormCfgRuleset : public QWidget
{
    Q_OBJECT

public:

    static const int REC_ADD;
    static const int REC_EDIT;
    static const int REC_DELETE;

    explicit FormCfgRuleset(QWidget *parent = 0);
    ~FormCfgRuleset();

    /**
      \brief Get data from column of current record by using the column name
      \param colName - name of column
      \return data contained in colName
      **************/
    virtual QVariant getColumnData(QString colName);

    /**
      \brief Get a reference to the ruleset view
      \return pointer to view
      *****************/
    virtual RulesetTableView *getView();

    /**
      \brief Get a reference to the ruleset model
      \return pointer to model
      *****************/
    virtual RulesetSqlTableModel *getModel() { return model; }


    virtual QVariant getUpdatedName();
    virtual void setUpdatedName(QVariant name);
    QVariant newName;

public slots:

    /**
    \brief When the form is displayed set the focus to the ruleset browse
     window and select the first row then calls currentRowChanged().

    \param event generated by QT
    ****************/
    virtual void showEvent(QShowEvent *event);

    /**
     \brief When the current row changes the ruleset is displayed in a
     readonly text box.
      *****************/
    virtual void currentRowChanged();

    /**
      \brief Calls routine to add a ruleset row
      ******************/
    virtual void slotBtnAdd();

    /**
      \brief Calls routine to edit a ruleset row
      ******************/
    virtual void slotBtnEdit();

    /**
      \brief Calls routine to delete a ruleset row
      ******************/
    virtual void slotBtnDelete();


    /**
      \brief Adds a rule snippet to a rule set.

      \param useInclude - if true include a link to the snippet
             else paste the contents of the snippet
      \param id - primary key of code snippet row
      \param name - name of snippet
      \param snippets - content of snippet
      ***************************/
    virtual void slotAddSnippet(bool useInclude, int id, QString name, QString snippets);



    virtual void slotRowsAboutToBeInserted(const QModelIndex &index, int start, int end);

signals:



protected:

    QPointer<RulesetSqlTableModel> model;
    QPointer<QSortFilterProxyModel> proxyModel;

    /**
      \brief Checks if current ruleset is the default ruleset as held on
             the system configuration table
      \return true if current ruleset is default else false.
      **********************/
    virtual bool isRulesetDefault();


private:
    Ui::FormCfgRuleset *ui;
};

#endif // FORMCFGRULESET_H
